package com.example.villagemarket;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;

import java.util.List;
import java.util.Map;


import com.example.villagemarket.adapter.ShoppingCartAdapter;
import com.example.villagemarket.adapter.ShoppingCartListView;

import com.haarman.listviewanimations.swinginadapters.prepared.ScaleInAnimationAdapter;



import android.os.Bundle;

import android.app.Activity;


import android.content.Context;

import android.content.Intent;


import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;



import android.widget.ListView;


public class ViewShoppingCart extends Activity {
	



	List<Map<String, String>> itemHolder;
	
	static TextView price;

	Context c;
	
	ScaleInAnimationAdapter ScaleInAnimationAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_shopping_cart);



		c = this;

		// Get listview to pass
		ListView list = (ListView) findViewById(R.id.cartList);
		
		ShoppingCartListView sclv = new ShoppingCartListView();
		itemHolder = new ArrayList<Map<String, String>>();
		for (Map.Entry<String, ArrayList<String>> entry : ShoppingCart.items
				.entrySet()) {
			String key = entry.getKey();
			itemHolder.add(addItem("item", entry.getValue(), key));

		}

		ArrayList<HashMap<String, String>> items = sclv.populateListView(
				itemHolder, list);

		ShoppingCartAdapter ad = new ShoppingCartAdapter(
				getApplicationContext(), items, this);

		ScaleInAnimationAdapter = new ScaleInAnimationAdapter(
				ad);

		ScaleInAnimationAdapter.setAbsListView(list);
		list.setAdapter(ScaleInAnimationAdapter);
		
		BigDecimal total = ShoppingCart.getTotal();
		
		price = (TextView)findViewById(R.id.price);
		if(total.equals(new BigDecimal(0)))
		{
			price.setText("$0.00");
		}
		else
		{
			price.setText("$"+total.toString());
		}
				
	}
	
	
	// helper method used to add items in data structure that holds the items
	private HashMap<String, String> addItem(String key, ArrayList<String> name,
			String description) {
		HashMap<String, String> x = new HashMap<String, String>();
		StringBuilder sb = new StringBuilder();
		sb.append(description + ":");
		for (String s : name) {
			sb.append(s);
			sb.append(":");
		}
		x.put(key, sb.toString());

		return x;
	}
	
	// checks if user has either wifi/mobile connection
	private boolean haveNetworkConnection() {
		boolean haveConnectedWifi = false;
		boolean haveConnectedMobile = false;

		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] netInfo = cm.getAllNetworkInfo();
		for (NetworkInfo ni : netInfo) {
			if (ni.getTypeName().equalsIgnoreCase("WIFI"))
				if (ni.isConnected())
					haveConnectedWifi = true;
			if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
				if (ni.isConnected())
					haveConnectedMobile = true;
		}
		return haveConnectedWifi || haveConnectedMobile;
	}
	
	 public void onBuyPressed(View v) {
	        // PAYMENT_INTENT_SALE will cause the payment to complete immediately.
	        // Chagne PAYMENT_INTENT_SALE to PAYMENT_INTENT_AUTHORIZE to only authorize payment and
	        // capture funds later.
		 
		 	BigDecimal total = ShoppingCart.getTotal();
		 	if(total.compareTo(new BigDecimal(0)) == 1 && haveNetworkConnection())
		 	{
				Intent i = new Intent(getApplicationContext(), PostCheckout.class);
				i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(i);
				
		 	}
		 	else if(!haveNetworkConnection())
		 	{
		 		Toast.makeText(getApplicationContext(), "There is no network connection!",
						Toast.LENGTH_SHORT).show();
		 	}
		 	else
		 	{
		 		Toast.makeText(getApplicationContext(), "There aren't any items in your cart!", Toast.LENGTH_SHORT).show();
		 	}
		 	


	    }

	    

		public static void updateTotal() {
			BigDecimal total = ShoppingCart.getTotal();
			
			if(total.equals(new BigDecimal(0)))
			{
				price.setText("$0.00");
			}
			else
			{
				price.setText("$"+total.toString());
			}
			
		}
		
		


}
